package com.fare.taluo.action.system.feedback;

import java.net.URL;

import javax.inject.Inject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.codehaus.xfire.client.Client;
import org.openkoala.auth.application.vo.ResourceVO;
import org.springframework.stereotype.Controller;

import com.fare.taluo.application.system.auth.MenusApplication;
import com.fare.taluo.auth.security.LogonUserCertificate;
import com.fare.taluo.base.struts2.json.BaseJsonStrutsAction;
import com.fare.taluo.conf.ConfigContainer;
import com.fare.taluo.conf.system.DefaultPropertiesLoader;
import com.fare.taluo.util.StringUtils;


@SuppressWarnings({ "serial"})
@Controller
@org.springframework.context.annotation.Scope("prototype")
@Namespace("/mffeedback")
@ParentPackage("json-default")
public class FeedbackAction extends BaseJsonStrutsAction  {
	private static final String PROJECT_FEEDBACK_AVAIL_TIME = "project.feedback.availTime";
	private static final String PROJECT_FEEDBACK_DOMAIN_NAME = "project.feedback.domainName";
	private static final String PROJECT_FEEDBACK_SECRET_KEY = "project.feedback.secretKey";
	//private static final String FEEDBACK_FAIL_MSG = "进入问题反馈失败";
	
	@Inject
	private MenusApplication menusApplication;
	//vo
	private String feedbackUrl = null, discussCode = null;
	/**
	 * 进入问题反馈
	 * @return
	 */
	@Action(value = "view", results = {@Result(name = "success", type="dispatcher", location="/pages/system/feedback/feedback.jsp") })
	public String view() {
		return SUCCESS;
	}
	
	@Action(value = "getUrl", results = { @Result(name = "JSON", type = "json", params = {
			"root", "feedbackUrl" }) })
	public String getUrl() throws Exception{
		String path = DefaultPropertiesLoader.getFeedBackUrl();
		
		String serviceURL = path + "/services/FbKeyApplication?wsdl";
		
		String secretKey = ConfigContainer.getInstance().get(PROJECT_FEEDBACK_SECRET_KEY, DefaultPropertiesLoader.CONFIG_FILE);
		
		String domainName = ConfigContainer.getInstance().get(PROJECT_FEEDBACK_DOMAIN_NAME, DefaultPropertiesLoader.CONFIG_FILE); 
		
		String availTime = ConfigContainer.getInstance().get(PROJECT_FEEDBACK_AVAIL_TIME, DefaultPropertiesLoader.CONFIG_FILE);
		
		String discussParentCode = null;
		
		String discussName = null;
		
		LogonUserCertificate user = com.fare.taluo.auth.security.ContextLoginUser.getLoginUser();
		ResourceVO  menu = null;
		if(StringUtils.isNotBlank(discussCode)){
			menu = menusApplication.find(discussCode);
			//discussParentCode = menu.getParentId();
			discussName = menu.getName();
		}
		String username = user==null?null:user.getUserName();

		if(username==null&&StringUtils.isNotBlank(discussCode)){
			this.feedbackUrl = "0";//限制登录用户才有自动创建讨论区权限
		}
		
		Client client = new Client(new URL(serviceURL));
		Object[] os = client.invoke("getUrl",
				/**
				 * 获取跳转到问题反馈的url 后面加‘*’必填 
				 * 
				 * secretKey* 密钥 ,与问题反馈系统对应
				 * 
				 * feekBackUrl* 问题反馈根路径
				 * 
				 * domainName* 该系统域名
				 * 
				 * discussParentCode 讨论区父级讨论区代码
				 * 
				 * discussCode  进入讨论区代码（如果没有将创建讨论区）
				 * 
				 * discussName （如果新增讨论区为必填） 进入讨论区名称
				 * 
				 * availTime（如果，传递username时，为必填） 生成口令有效时间（毫秒）,口令生成后超过该设定时间，将无效，不能再通过该口令登录。
				 * 
				 * username 第三方系统用户名（如果没有将创建用户，密码默认）
				 * public String getUrl
				 */
			new String[]{secretKey
				,path+"/help/topic/index.action"
				, domainName
				, discussParentCode
				, discussCode
				, discussName
				, availTime
				,	username});
		if(os!=null&&os.length!=0){
			this.feedbackUrl = os[0].toString();
		}else{
			this.feedbackUrl = "-1";
		}
		return "JSON";
	}

	public String getDiscussCode() {
		return discussCode;
	}

	public void setDiscussCode(String discussCode) {
		this.discussCode = discussCode;
	}

	public void setFeedbackUrl(String feedbackUrl) {
		this.feedbackUrl = feedbackUrl;
	}

	public String getFeedbackUrl() {
		return feedbackUrl;
	}
}
